/* 
reduce接收两个参数: 一个回调函数(reducer)和一个初始值(val);
    arr.reduce(reducer,val);

reducer接收四个参数: 
*/

// 1. 累加数组中的每一项
const arr1 = [1, 2, 3, 4];
let num1 = arr1.reduce(((pre, cur) => pre + cur), 0);
console.log(num1);

// 2. 累加对象中的每一项;
const arr2 = [{ x: 1 }, { x: 2 }, { x: 3 }];
let num2 = arr2.reduce(((pre, cur) => pre + cur.x), 0);
console.log(num2);

// 数组concat方法: 合并多个数组; (返回一个新数组,不改变原始数组);
let t1 = [1, 2, 3];
let t2 = [2, 3, 4];
let t3 = ['jingwen', ''];
console.log(t1.concat(t2).concat(t3));


// 3. 把二维数组转化为一维数组
const arr3 = [[1], [2], ['jingwen']];
let num3 = arr3.reduce(((acc, cur) => acc.concat(cur)), []);
console.log(num3);
